package ui;

/**
 * EDCDirChooserPanel
 * This view allows to set up the directory in which we want to analyze DTDs, on local hard drive
 */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import utils.EDCResource;


public class EDCDirChooserPanel extends JPanel

implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static private String newline = "\n";
	private JLabel lblFolderName;
	private JTextField txtFolderName;
	private JTextArea log;
	private JFileChooser fc;

	public EDCDirChooserPanel(String label, String bttxt) {
		super();

		add(new JSeparator(SwingConstants.VERTICAL));
		add(Box.createHorizontalStrut(5));

		//Create the log first, because the action listener
		//needs to refer to it.
		/*
		log = new JTextArea(5,20);
		log.setMargin(new Insets(5,5,5,5));
		log.setEditable(false);
		JScrollPane logScrollPane = new JScrollPane(log);
		 */
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		setLayout(gridbag);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		
		lblFolderName = new JLabel(label);
		add(lblFolderName, c);
		
		c.gridx = 0;
		c.gridy = 1;

		txtFolderName = new JTextField(20);
		add(txtFolderName, c);
		
		c.gridx = 1;
		c.gridy = 1;

		JButton sendButton = new JButton(bttxt);
		sendButton.addActionListener(this);

		add(sendButton, c);
//		add(logScrollPane, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent e) {
		//Set up the file chooser.
		if (fc == null) {
			fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

//			System.out.println("folder: "+EDCResource.USER_DIR);
			fc.setCurrentDirectory(new File(EDCResource.USER_DIR));


		}

		//Show it.
		int returnVal = fc.showDialog(EDCDirChooserPanel.this,
		"Choose Directory");


		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
//			EDCResource.dtdFolderName = file.getName();
//			EDCResource.dtdFolderPath = file.getAbsolutePath();
			try {
				EDCResource.setInput(file.getCanonicalPath());
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			
			txtFolderName.setText(file.getAbsolutePath());
			
//			log.append("Attaching file: " + file.getName()
//			+ "." + newline);
		} 
//		else {
//		log.append("Attachment cancelled by user." + newline);
//		}
//		log.setCaretPosition(log.getDocument().getLength());

//		//Reset the file chooser for the next time it's shown.
		fc.setSelectedFile(null);

	}

	public JTextField getTxtFolderName() {
		return txtFolderName;
	}

	public void setTxtFolderName(JTextField txtFolderName) {
		this.txtFolderName = txtFolderName;
	}

}
